package com.ruoyi.contract.controller;

import com.ruoyi.contract.pojo.Contract;
import com.ruoyi.contract.pojo.Resuit;
import com.ruoyi.contract.service.ContractService;
import com.ruoyi.contract.utlis.PDFUtli;
import com.ruoyi.contract.utlis.SendmailUtli;
import com.ruoyi.contract.utlis.WatermarkUtli;
import javafx.scene.control.TableRow;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;


@RestController
@RequestMapping("contract")
public class ContractController extends  Exception {


	@Resource
	RedisTemplate redisTemplate;

	@Resource
	ContractService contractService;


	/**
	 * 添加合同到sql数据库
	 * @param contract
	 * @return
	 */
	@RequestMapping("insertContract")
	public Resuit insertContract(@RequestBody Contract contract){
		try {
			contractService.insertContract(contract);

			return new Resuit(200,"添加成功",null);
		}catch (Exception e){
			e.getMessage();
		}
		return  new Resuit(250,"添加失败",null);
	}


	/**
	 * 查询合同表
	 * @param contract
	 * @return
	 */
	@RequestMapping("selectContract")
	public  Resuit selectContract(@RequestBody Contract contract){
			List<Contract> contractList =contractService.selectContract(contract);
			return  new Resuit(200,"查询成功",contractList);

	}


	/**
	 * 根据id进行删除合同
	 * @param cid
	 * @return
	 */
	@RequestMapping("deleteContract/{cid}")
	public  Resuit deleteContract(@PathVariable("cid") long cid){
		try {
			contractService.deleteById(cid);
			return new Resuit(200,"删除成功",null);
		}catch (Exception e){
				e.getMessage();
		}
		return new Resuit(250,"删除失",null);
	}


	/**
	 * 修改合同
	 * @param contract
	 * @return
	 */
	@RequestMapping("updateByid")
	public  Resuit updateByid(@RequestBody Contract contract){
			contractService.updateById(contract);
			return  new Resuit(200,"修改成功",null);
	}





	/**
	 * 暂时存到redis当中
	 * @param contract
	 */
	@RequestMapping("redisAddContract")
	public  void  redisAddContract(@RequestBody Contract contract){
	}


	/**
	 * 生成pdf文件并通过客户邮箱进行发送
	 * @param contract
	 * @throws Exception
	 */
	@RequestMapping("test")
	public  void  test(Contract contract) throws Exception {
		List<Contract> contractList = contractService.selectContract(null);
		for (Contract contract1 : contractList) {
			String mailbox="2030065989@qq.com";
			PDFUtli.generatePDFUtli(contract1);
			WatermarkUtli.addPDFWaterMark();
			SendmailUtli.sendmail(mailbox);
		}






	}




}
